/*
 * 文件名： ScoreShopFragment.java
 * 描    述：MainActivity -- 积分商城
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.miguo.activity.base.BaseFragment;
import com.miguo.activity.callback.IFragment;
import com.miguo.app.AppConfig;
import com.miguo.app.AppContext;
import com.miguo.app.AppHelper;
import com.miguo.component.BaseHeadView;
import com.miguo.framework.view.pagerindicator.TabPageIndicator;
import com.miguo.framework.view.pagerindicator.UnderlinePageIndicator;
import com.miguo.plugins.Statistics;
import com.miguo.ui.R;

import java.util.ArrayList;

public class ScoreShopFragment extends BaseFragment implements IFragment {

    private View contentView;
    private TextView mScoreTv;
    private ViewPager mPagerView;
    private TabPageIndicator mTabPageIndicator;
    private UnderlinePageIndicator mLineIndicator;
    private String[] titles = {"抽奖", "0元兑"};

    private BaseHeadView mBaseHead;

    private ArrayList<Fragment> fragment;

    public static ScoreShopFragment newInstance() {
        ScoreShopFragment fragment = new ScoreShopFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_score_shop,
                container, false);
        return contentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        mScoreTv = (TextView) contentView.findViewById(R.id.score_tv);
        // 标题
        mBaseHead = (BaseHeadView) contentView.findViewById(R.id.base_head);
        mBaseHead.setActivity(getActivity());
        mBaseHead.setTitle("积分商城");

        mBaseHead.getLeftView().setVisibility(View.INVISIBLE);

        // 签到
        mBaseHead.getRightView().setVisibility(View.VISIBLE);
        mBaseHead.getRightView().setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.StartSign(getActivity());
            }
        });

        if (!AppContext.UserInfo().getIsLogin()) {
            mScoreTv.setVisibility(View.GONE);
        } else {
            mScoreTv.setVisibility(View.VISIBLE);
            mScoreTv.setText("您的当前积分是:" + AppContext.UserInfo().getScore());
        }

        fragment = new ArrayList<Fragment>();
        ScoreShopLotteryFragment lottery = ScoreShopLotteryFragment.newInstance();
        fragment.add(lottery);
        ScoreShopExchangeFragment exchange = ScoreShopExchangeFragment.newInstance();
        fragment.add(exchange);

        mPagerView = (ViewPager) contentView.findViewById(R.id.pager_view);
        FragmentPagerAdapter mPagerAdapter = new PagerAdapter(getChildFragmentManager());
        mPagerView.setAdapter(mPagerAdapter);

        mLineIndicator = (UnderlinePageIndicator) contentView
                .findViewById(R.id.line_indicator);
        mLineIndicator.setViewPager(mPagerView);

        mTabPageIndicator = (TabPageIndicator) contentView.findViewById(R.id.tab_indicator);
        mTabPageIndicator.setViewPager(mPagerView);
        mTabPageIndicator.setOnPageChangeListener(mLineIndicator);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!AppContext.UserInfo().getIsLogin()) {
            mScoreTv.setVisibility(View.GONE);
        } else {
            mScoreTv.setVisibility(View.VISIBLE);
            mScoreTv.setText("您的当前积分是:" + AppContext.UserInfo().getScore());
        }
    }

    @Override
    public void onStartFragment() {
        Statistics.onTrackCustomClick(getActivity(), AppConfig.STATISTICS.EVENT_NAME_4);
    }

    @Override
    public void onStopFragment() {

    }

    private class PagerAdapter extends FragmentPagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragment.get(position);
        }

        @Override
        public int getCount() {
            return fragment.size();
        }

        //顶部的title
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position % titles.length].toUpperCase();
        }

    }

}
